package com.css.fxfzdpzs.modules.hddc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.fxfzdpzs.base.remote.RemoteService;
import com.css.fxfzdpzs.modules.hddc.entity.first.DifferentConut;
import com.css.fxfzdpzs.modules.hddc.entity.first.FaultNum;
import com.css.fxfzdpzs.modules.hddc.entity.first.FaultTotal;
import com.css.fxfzdpzs.modules.hddc.entity.second.FaultNumLen;
import com.css.fxfzdpzs.modules.hddc.repository.HddcRepository;
import com.css.fxfzdpzs.modules.hddc.service.HddcService;
import com.css.fxfzdpzs.modules.hddc.vo.FaultNumVo;
import com.css.fxfzdpzs.modules.xczh.entity.first.DisasterVisitsCount;
import com.css.fxfzdpzs.modules.xczh.entity.second.XcResultDate;
import com.css.fxfzdpzs.modules.xczh.repository.XczhRepository;
import org.apache.xpath.SourceTree;
import org.slw.common.helper.PropsHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class HddcServiceImpl implements HddcService {
    @Autowired
    private HddcRepository hddcRepository;
    @Autowired
    private XczhRepository xczhRepository;
    @Resource
    private RemoteService remoteService;

    @Override
    public List<DisasterVisitsCount> getProvinceRank(String system) {
        List<DisasterVisitsCount> list = xczhRepository.getVisitNum(system);
        list = list.stream().sorted(Comparator.comparing(DisasterVisitsCount::getAccumulateVisitNum).reversed()).collect(Collectors.toList()).subList(0, Math.min(list.size(), 3));
        return list;
    }

    @Override
    public Map<String, Object> getVisitNum(String system) {
        List<DisasterVisitsCount> list = xczhRepository.getVisitNum(system);
        Map<String, Object> map = new HashMap<>();
        int todaySum = list.stream().mapToInt(DisasterVisitsCount::getTodayVisitNum).sum();
        int accumulateSum = list.stream().mapToInt(DisasterVisitsCount::getAccumulateVisitNum).sum();
        map.put("todaySum", todaySum);
        map.put("accumulateSum", accumulateSum);
        return map;
    }

    @Override
    public List<FaultNum> getFaultNum() {
        List<FaultNum> list = hddcRepository.getFaultNum();
        return list;
    }

    @Override
    public FaultNumVo getFaultTotal() {
        return hddcRepository.getFaultTotal();
    }

    @Override
    public List<DifferentConut> getDifferentConut() {
        return hddcRepository.getDifferentConut();
    }

    @Override
    public DisasterVisitsCount getVisitNumByPro(String province) {
        return xczhRepository.getVisitNumByPro(province);
    }

    @Override
    public Integer getFaultNumByPro(String province) {
        return hddcRepository.getFaultNumByPro(province);
    }

    @Override
    public List<FaultNumLen> getFaultNumLenByPro(String province) {
        return hddcRepository.getFaultNumLenByPro(province);
    }

    @Override
    public List<DifferentConut> getDifferentConutByPro(String province) {
        return hddcRepository.getDifferentConutByPro(province);
    }

    @Override
    public Map<String, List<FaultNumVo>> getGeom() {
        List<FaultNumVo> list = hddcRepository.getGeom();
        List<FaultNumVo> list1 = new ArrayList<>();
        List<FaultNumVo> list2 = new ArrayList<>();
        List<FaultNumVo> list3 = new ArrayList<>();
        List<FaultNumVo> list4 = new ArrayList<>();
        for (FaultNumVo faultNumVo : list) {
            Integer num = faultNumVo.getNum();
            if (num > 0 && num <= 50) list1.add(faultNumVo);
            if (num > 50 && num <= 150) list2.add(faultNumVo);
            if (num > 150 && num <= 300) list3.add(faultNumVo);
            if (num > 300) list4.add(faultNumVo);
        }
        Map<String, List<FaultNumVo>> map = new HashMap<>();
        map.put("0",list1);
        map.put("1",list2);
        map.put("2",list3);
        map.put("3",list4);
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void dataSync() {
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String hddcUrl = configGroup.getProperty("hddcRestUrl");

        //各省（直辖市）断层段数量情况统计数据
        String provinceStatUrl = hddcUrl + "/faultData/getProvinceFaultNumStat";

        Object provinceStaData = remoteService.getData(provinceStatUrl, 3,  new JSONObject()).getData();
        if(provinceStaData!=null){
            hddcRepository.delFaultNum();
            JSONArray jsonArray = (JSONArray) provinceStaData;
            List<FaultNum> FaultNumList = JSON.parseArray(jsonArray.toJSONString(), FaultNum.class);
            hddcRepository.saveFaultNum(FaultNumList);

        }

        //不同活动时代统计
        String faultTypeStatUrl = hddcUrl + "/faultData/getFaultTypeStat";

        Object faultTypeStatData = remoteService.getData(faultTypeStatUrl, 3,  new JSONObject()).getData();
        if(faultTypeStatData!=null){
            hddcRepository.delDifferentConut();
            JSONArray jsonArray = (JSONArray) faultTypeStatData;
            List<DifferentConut> differentConutList = JSON.parseArray(jsonArray.toJSONString(), DifferentConut.class);
            hddcRepository.saveDifferentConut(differentConutList);

        }


        //各市断层段数量情况统计数据
        String cityStaUrl = hddcUrl + "/faultData/getCityFaultNumStat";

        Object cityStaData = remoteService.getData(cityStaUrl, 3,  new JSONObject()).getData();
        if(cityStaData!=null){
            hddcRepository.delFaultNumLen();
            JSONArray jsonArray = (JSONArray) cityStaData;
            List<FaultNumLen> faultNumLenList = JSON.parseArray(jsonArray.toJSONString(), FaultNumLen.class);
            hddcRepository.saveFaultNumLen(faultNumLenList);

        }
    }
}
